#include <iostream>
#include <fstream>
#include <list>
#include <algorithm>
#include <string>

using namespace std;

// Split a string into a vector of substrings
list<string> split(const string& s, const string& delim, const bool keep_empty = true);

int main(int argc, char* argv[]) 
{
	int numberOfVariable, numberOfFormula;
	ifstream inputFile("input.in", ios::in);
	if (!inputFile) {
		printf("file input.in not found\n");
		return -1;
	}

	inputFile >> numberOfVariable >> numberOfFormula;
	if (numberOfVariable == 0 || numberOfFormula == 0) {
		printf("Wrong input parameters\n");
		return -1;
	}

	int i;
	int **matrix = new int*[numberOfFormula];
	for (i = 0; i < numberOfFormula; i++) {
		matrix[i] = new int[numberOfVariable];
	}

	
	string formula;
	// Eliminate the return of the last line
	getline(inputFile, formula);
	for (i = 0; i < numberOfFormula; i++) {
		getline(inputFile, formula);
		list<string> substring = split(formula, " ");
		list<string>::reverse_iterator iter = substring.rbegin();
		// Skip over the last zero number
		++iter;
		int j = numberOfVariable - 1;
		while (iter != substring.rend()) {
			if ((*iter).empty()) {
				matrix[i][j--] = 0;
				++iter;
			} else {
				matrix[i][j--] = atoi((*iter).c_str());
			}
			++iter;
		}		
	}

	inputFile.close();
	for (i = 0; i < numberOfFormula; i++) {
		delete[] matrix[i];
	}
	delete[] matrix;
	return 0;
}

// Split a string into a vector of substrings
list<string> split(const string& s, const string& delim, const bool keep_empty) {
    list<string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}